package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.ExdeDutyArea;
import com.sl.au.entity.Module;
import com.sl.au.entity.User;
import com.sl.au.service.ExdeInsideConfigurationService;
import com.sl.au.service.OperateLogService;

/**
 * 
 * @author yry
 * 2017-03-06 内勤-片区配置Controller 
 *
 */
@RestController
@RequestMapping(value="/ui/insideconfiguration")
public class ExdeInsideConfigurationController extends BaseController {
	
	@Autowired
	ExdeInsideConfigurationService exdeInsideConfigurationService;
	
	@Autowired
	OperateLogService operateLogService;
	
	/***
	 * 进入列表界面
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(){
		Module module = this.getPageModule("insideconfigurationindex");
        ModelAndView modelAndView=createLayoutView("insideconfiguration/insideconfigurationindex");
        Account a = this.getMyAccount();
        modelAndView.addObject("user", exdeInsideConfigurationService.getAllUserList(a.getUser().getSaleArea().getSuperior()));
        modelAndView.addObject("saleArea", exdeInsideConfigurationService.getAllSaleAreaList(a.getUser().getSaleArea().getSuperior()));
        modelAndView.addObject("superior", a.getUser().getSaleArea().getSuperior());
        modelAndView.addObject("module", module);
        return modelAndView;
    }
	
	/**
	 * 删除
	 */
	@RequestMapping(value="delete",method = RequestMethod.GET)
	public Map<String, Object> deleteBeforeContract(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		exdeInsideConfigurationService.deleteExdeDutyAreaById(id);
		map.put("code", 0);
		return map;
	}
	
	/***
	 * 获取内勤-片区配置列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		String superior = a.getUser().getSaleArea().getSuperior();
		System.out.println(superior);
		String name = "contractbefore";
		Module module = this.getPageModule(name);
		String dataFilter = module.getDataFilter();
		if (key == null) {
			key = "";
		}
		if (userId == null) {
			userId = "";
		}
		if (superior == null) {
			superior = "";
		}
		if (dataFilter == null) {
			dataFilter = "";
		}
		condition.put("key", key);
		condition.put("userId", userId);
		condition.put("superior", superior);
		condition.put("dataFilter", dataFilter);
		List<ExdeDutyArea> list =new ArrayList<ExdeDutyArea>();
		if(key != ""){
			list = exdeInsideConfigurationService.getExdeDutyAreaList(key,superior);
		}else{
			list = exdeInsideConfigurationService.getAllExdeDutyAreaList(superior);
		}
		long count = list.size();
		map.put("data", list);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public Map<String, Object> saveExdeBeforeContract(HttpServletRequest request, @RequestBody ExdeDutyArea exdeDutyArea) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		String superior = a.getUser().getSaleArea().getSuperior();
		boolean isInit = false;
		String uuid = java.util.UUID.randomUUID().toString();
		if(!StringUtils.hasText(exdeDutyArea.getId())) {
			isInit = true;
			exdeDutyArea.setId(uuid);
		}
		List<ExdeDutyArea> list = exdeInsideConfigurationService.getAllExdeDutyAreaList(superior);
		int code = 0;//不重复
		for(int i=0;i<list.size();i++){
			if(list.get(i).getId().equals(exdeDutyArea.getId()) && list.get(i).getDutyPersonName().equals(exdeDutyArea.getDutyPersonName()) && list.get(i).getAreaName().equals(exdeDutyArea.getAreaName()) && list.get(i).getType().equals(exdeDutyArea.getType())){
				code = 1;//代表未修改时点击保存
				break;
			}
			if(!list.get(i).getId().equals(exdeDutyArea.getId())){//不是他自己本身
				if(list.get(i).getAreaName().equals(exdeDutyArea.getAreaName()) && list.get(i).getType().equals(exdeDutyArea.getType())){
					code = 2;//片区和类型相等
					break;
				}
			}
			if(list.get(i).getId().equals(exdeDutyArea.getId())){
				if(list.get(i).getAreaName().equals(exdeDutyArea.getAreaName()) && list.get(i).getType().equals(exdeDutyArea.getType())){
					code = 2;//片区和类型相等
					break;
				}
			}
		}
		if(code==0){
			exdeInsideConfigurationService.getexdeInsideConfigurationRepository().save(exdeDutyArea);
			//保存日志
			String id=exdeDutyArea.getId();
			User user=a.getUser();
			String cContractCode=exdeDutyArea.getAreaName();
			String content = "新增、编辑内勤-片区配置信息【"+cContractCode+"】";
			operateLogService.log(user, "内勤-片区配置新增、编辑", content);
			map.put("isInit", isInit);
			map.put("id", id);
		}
		map.put("code", code);
		return map;
	}

}
